package com.ruoyi.qiushi.service.mall.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qiushi.domain.mall.ProductSpu;
import com.ruoyi.qiushi.domain.mall.ProductSpuFile;
import com.ruoyi.qiushi.mapper.mall.ProductSpuFileMapper;
import com.ruoyi.qiushi.mapper.mall.ProductSpuMapper;
import com.ruoyi.qiushi.service.mall.IProductSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-08
 */
@Service
public class ProductSpuServiceImpl implements IProductSpuService {
    @Autowired
    private ProductSpuMapper productSpuMapper;
    @Autowired
    private ProductSpuFileMapper productSpuFileMapper;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public ProductSpu selectProductSpuById(Long id) {
        ProductSpu productSpu = productSpuMapper.selectProductSpuById(id);

        ProductSpuFile productSpuFile = new ProductSpuFile();
        productSpuFile.setSpuId(productSpu.getId());
        List<ProductSpuFile> productSpuFileList = productSpuFileMapper.selectProductSpuFileList(productSpuFile);

        StringBuilder imageList = new StringBuilder();

        for (int i = 0; i < productSpuFileList.size(); i++) {
            imageList.append(productSpuFileList.get(i).getUrl());
            if (i != productSpuFileList.size() - 1) imageList.append(",");
        }

        productSpu.setImageList(imageList.toString());
        return productSpu;
    }

    /**
     * 查询商品列表
     *
     * @param productSpu 商品
     * @return 商品
     */
    @Override
    public List<ProductSpu> selectProductSpuList(ProductSpu productSpu) {
        return productSpuMapper.selectProductSpuList(productSpu);
    }

    /**
     * 新增商品
     *
     * @param productSpu 商品
     * @return 结果
     */
    @Override
    public int insertProductSpu(ProductSpu productSpu) {
        String[] imageArray = productSpu.getImageList().split(",");

        productSpu.setImage(imageArray[0]);
        productSpu.setCreateTime(DateUtils.getNowDate());
        productSpuMapper.insertProductSpu(productSpu);

        for (String image : imageArray) {
            ProductSpuFile productSpuFile = new ProductSpuFile();
            productSpuFile.setSpuId(productSpu.getId());
            productSpuFile.setUrl(image);
            productSpuFile.setType("1");
            productSpuFile.setCreateTime(new Date());
            productSpuFile.setUpdateTime(new Date());

            productSpuFileMapper.insertProductSpuFile(productSpuFile);
        }

        return 1;
    }

    /**
     * 修改商品
     *
     * @param productSpu 商品
     * @return 结果
     */
    @Override
    public int updateProductSpu(ProductSpu productSpu) {
        productSpuFileMapper.deleteBySpuId(productSpu.getId());

        if (productSpu.getImageList() != null && productSpu.getImageList().length() > 0) {
            String[] imageArray = productSpu.getImageList().split(",");

            productSpu.setImage(imageArray[0]);
            productSpu.setUpdateTime(DateUtils.getNowDate());
            productSpuMapper.updateProductSpu(productSpu);

            for (String image : imageArray) {
                ProductSpuFile productSpuFile = new ProductSpuFile();
                productSpuFile.setSpuId(productSpu.getId());
                productSpuFile.setUrl(image);
                productSpuFile.setType("1");
                productSpuFile.setCreateTime(new Date());
                productSpuFile.setUpdateTime(new Date());

                productSpuFileMapper.insertProductSpuFile(productSpuFile);
            }
        }

        return 1;
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductSpuById(Long id) {
        ProductSpu productSpu = new ProductSpu();
        productSpu.setId(id);
        productSpu.setDelFlag("1");
        return productSpuMapper.updateProductSpu(productSpu);
    }
}
